<template>
  <div>
    <Teleport to="body">
      <!-- 蒙版 -->
      <transition name="fade">
        <div
          v-if="modelValue"
          class="w-screen h-screen bg-zinc-900/80 z-40 fixed top-0 left-0"
          @click="emits('update:modelValue', false)"
        ></div>
      </transition>
      <!-- 内容 -->
      <transition name="popup-down-up">
        <div
          v-if="modelValue"
          class="w-screen bg-white dark:bg-zinc-800 z-50 fixed bottom-0"
          v-bind="$attrs"
        >
          <slot />
        </div>
      </transition>
    </Teleport>
  </div>
</template>

<script setup>
import { watch } from 'vue'
import { useScrollLock } from '@vueuse/core'
// 开可以通过vueUse的useVModel来简化
// 1. 传入props
// 2. 传入emit
// 3. 返回一个响应式的变量
// 4. 这个变量可以直接使用
// 5. 这个变量可以直接修改
// const isVisible = useVModel(props)

const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true,
    default: false
  }
})
const emits = defineEmits(['update:modelValue'])
// 锁定滚动
const isLocked = useScrollLock(document.body)
watch(
  () => props.modelValue,
  (val) => {
    isLocked.value = val
  },
  { immediate: true }
)
</script>

<style lang="scss" scoped>
.fade-enter-active,
.fade-leave-active {
  transition: all 0.3s;
}
// 准备进入，离开完成
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.popup-down-up-enter-active,
.popup-down-up-leave-active {
  transition: all 0.3s;
}
// 准备进入，离开完成
.popup-down-up-enter-from,
.popup-down-up-leave-to {
  opacity: 0;
  transform: translateY(100%);
}
</style>
